<?php
	include("../lib/DataProvider.php");
	if(isset($_POST['btnThem']))
	{
		$TenCH= $_POST['txtTenCH'];
		$MaLSP=$_POST['txtMaLSP'];
		
		//tạo mã loại
		$soluong=DataProvider::ExecuteQuery("select * from cauhinh");
		$stt=mysql_num_rows($soluong)+1;
		$MaCH=sprintf("%05s", $stt);
		$flag=true;
		while($flag==true)
		{
			$ds=DataProvider::ExecuteQuery("select * from cauhinh where MACH = '".$MaCH."'");
			if(mysql_num_rows($ds)>0)
			{
				$stt=$stt+1;
				$MaCH=sprintf("%05s", $stt);
				$flag=true;
			}else $flag=false;
		}
		//insert
		$insert="insert into cauhinh(MACH,TENCH,MALSP) values('".$MaCH."','".$TenCH."','".$MaLSP."')";
		$result=DataProvider::ExecuteQuery($insert);
		echo '<div class="center"><b>Thêm cấu hình thành công!</b></div>';
		?>
			<head>
			<meta http-equiv="refresh" content="2; url=?act=4" />
			</head>
		<?php
		return;
	}
	if(isset($_POST['btnCapNhat']))
	{
		$MaCH=$_POST['txtMaCH'];
		$TenCH=$_POST['txtTenCH'];
		$MaLSP=$_POST['txtMaLSP'];
		$BiXoa=0;
		if(isset($_POST['cbBiXoa']))
			$BiXoa=1;
		$update="update cauhinh set TENCH='".$TenCH."',MALSP='".$MaLSP."',BIXOA=".$BiXoa." where MACH='".$MaCH."'";
		DataProvider::ExecuteQuery($update);
		echo '<div class="center"><b>Cập nhật thành công!</b></div>';
		?>
			<head>
			<meta http-equiv="refresh" content="2; url=?act=4" />
			</head>
		<?php
		return;
	}
?>